package gui;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Stack;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import model.Job;
import model.Role;
import model.User;
import controller.Controller;

/**
 * The main window that handles UI. Responsible for coordinating all user
 * dialogs.
 * 
 * @author James Gunter - jpgunter
 */
public class MainWindow extends WindowAdapter {

  protected static final int DO_NOTHING_ON_CLOSE = 0;

  private Controller my_controller;

  private JFrame my_main_frame;

  private User my_user;

  private Stack<JPanel> my_panel_history;

  /**
   * Constructor.
   * 
   * @param the_controller
   *          Controller object.
   */
  public MainWindow(Controller the_controller) {
    my_controller = the_controller;
    my_panel_history = new Stack<JPanel>();
  }

  /**
   * Shows the MainWindow.
   */
  public void show() {
    my_main_frame = new JFrame("Urban Park Management System");
    // my_main_frame.setJMenuBar(createParksMenu());
    my_main_frame.addWindowListener(this);
    my_main_frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

    changePanel(new LoginPanel(this));

  }

  /**
   * Gets main window from outside of MainWindow class.
   * 
   * @return
   */
  public JFrame getMyMainFrame() {
    return my_main_frame;

  }

  private void pushPanel(JPanel the_panel) {
    my_panel_history.push((JPanel) my_main_frame.getContentPane());
    changePanel(the_panel);
  }

  private void changePanel(JPanel the_panel) {
    my_main_frame.setVisible(false);
    my_main_frame.setContentPane(the_panel);
    my_main_frame.pack();
    my_main_frame.setVisible(true);
  }
  
  /**
   * Goes back one panel.
   */
  public void back() {
    changePanel(my_panel_history.pop());
  }
  
  /**
   * Goes all the way back to the home panel.
   */
  public void home() {
    my_panel_history.clear();
    changePanel(new UserPanel(this, my_user));
  }
  
  /**
   * Tries to login the user. Will display a JOptionPane if the login fails.
   * 
   * Once logged in it will take the user to their home Panel.
   * @param the_e_mail The e_mail of the user trying to login.
   */
  public void doLogin(String the_e_mail) {

    my_user = my_controller.getUserByEmail(the_e_mail);
    if (my_user == null) {
      JOptionPane.showMessageDialog(my_main_frame, "Invalid E-Mail");
    } else {
      if(my_user.getUserRole() == Role.BLACKBALLED){
        JOptionPane.showMessageDialog(my_main_frame, "You may not login. Please contact Urban Parks in order to volunteer");
      } else {
        changePanel(new UserPanel(this, my_user));
      }
    }

  }
  /**
   * Logout the current user.
   */
  public void doLogout() {
    my_user = null;
    my_panel_history.clear();
    changePanel(new LoginPanel(this));
  }
  
  /**
   * Show a single Job in a JobView.
   * @param the_job The job to show.
   */
  public void showJob(Job the_job) {
    pushPanel(new JobView(this, the_job, my_controller, my_user));
  }
  
  /**
   * Shows a list of jobs based on the current user.
   */
  public void showMyJobsList() {
    pushPanel(new JobListView(this, my_controller, my_controller.getJobs(my_user)));
  }
  
  /**
   * Shows a list of all jobs in the system.
   */
  public void showAllJobsList() {
    pushPanel(new JobListView(this, my_controller, my_controller.getJobs()));
  }
  
  /**
   * Shows a list of only pending jobs.
   */
  public void showPendingJobsList(){
    pushPanel(new JobListView(this, my_controller, my_controller.getPendingJobs()));
  }
  
  /**
   * Show the Calendar view of jobs.
   */
  public void showCalendar() {
    pushPanel(new CalendarView(this, my_controller));
  }
  
  /**
   * Shows a list of all the volunteers not blackballed.
   */
  public void showVolunteerList() {
    pushPanel(new VolunteerListView(this, my_controller.getVolunteers()));
  }
  
  /**
   * Shows a specific volunteer.
   * @param the_volunteer
   */
  public void showVolunteer(User the_volunteer){
    pushPanel(new VolunteerView(this, the_volunteer));
  }
  /**
   * Shows the Job Request Form.
   */
  public void showJobForm() {
    pushPanel(new JobForm(this, my_controller, my_user));
  }
  
  /**
   * Shows a list of all blackballed volunteers.
   */
  public void showBlackballedList() {
    pushPanel(new VolunteerListView(this, my_controller.getBlackballed()));
  }
  
  /**
   * Method that is performed when a window is closed.
   * 
   * Stores all the data out to the xml file.
   */
  @Override
  public void windowClosed(WindowEvent e) {
    my_controller.shutdown();
  }

  

}
